#pragma once

#define LOKI_OBJECT_LEVEL_THREADING
#include "Threads.h"
#include "TinyThread.h"

#include <map>
namespace TinyCC
{
	class CTinyThreadPool : public Loki::ObjectLevelLockable<CTinyThreadPool>
	{
	public:
		CTinyThreadPool(int nMaxThreadCount);
		virtual ~CTinyThreadPool(void);
	private:
		long m_nMaxThreadCount;
		long m_nCurAllThreadCount;
		long m_nCurWorkThreadCount;

	protected:
		HANDLE m_recHandle; 
		HANDLE m_hAllDeadEvent;

	private:
		void _init(void);
		void _destroy(void);
	public:
		int  getMaxThreadCount(void){return (int)m_nMaxThreadCount;};
		int  getCurAllThreadCount(void){return (int)m_nCurAllThreadCount;};
		void setRecHandle( HANDLE hRecEvent ){	m_recHandle = hRecEvent;}
		HANDLE getRecHandle(){return m_recHandle;}
		void increaseThread(){_incrementCurWorkThreadCount();}
		void decreaseThread(){_decrementCurWorkThreadCount();}
		HANDLE& getAllDeadEvent(){return m_hAllDeadEvent;}

		template <typename PointerToObj, typename PointerToMemberFunc >
		CTinyThread* createNewThread(const PointerToObj& pObj, PointerToMemberFunc pMemberFunc, void* param)
		{
			if(m_nCurAllThreadCount>=m_nMaxThreadCount)
				return NULL;
			DWORD dwThreadIdTemp=0;
			CTinyThread* pThread = new CTinyThread( pObj, pMemberFunc, param );
			return pThread;
		}


	protected:
		void  _setMaxThreadCount(int nMaxThreadCount){m_nMaxThreadCount=nMaxThreadCount;};
		int  _getCurWorkThreadCount(void){return (int)m_nCurWorkThreadCount;};

		void _incrementCurAllThreadCount(void);
		void _decrementCurAllThreadCount(void);
		void _setCurAllThreadCount(int nCount);


		void _incrementCurWorkThreadCount(void);
		void _decrementCurWorkThreadCount(void);
		void _setCurWorkThreadCount(int nCount);

		
	public:
		std::map<DWORD,CTinyThread*> m_threadMap;
	};
}